//
//	LayeredWindow.c
//
//  Copyright (c) 2002 by J Brown 
//  Freeware
//
//  BOOL SetWindowTrans(HWND hwnd, int percent)
//  BOOL RemoveWindowTrans(HWND hwnd)
//
//	These two functions provide a wrapper around
//  the new SetLayeredWindowAttributes API call (under Win2000/XP)
//
//	These functions dynamically link to the new API call (if present). 
//  If running under win9x / NT, then the function will
//  fail gracefully.
//

#include <windows.h>
#include <tchar.h>

#ifndef WS_EX_LAYERED
#define WS_EX_LAYERED  0x00080000
#endif

// Prototype for Win2000/XP API: SetLayeredWindowAttributes
typedef BOOL (WINAPI * SLWAProc)(HWND hwnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);

#define LWA_COLORKEY            0x00000001
#define LWA_ALPHA               0x00000002

#define ULW_COLORKEY            0x00000001
#define ULW_ALPHA               0x00000002
#define ULW_OPAQUE              0x00000004


BOOL SetWindowTrans(HWND hwnd, int percent)
{
	SLWAProc pSLWA;
	HMODULE  hUser32;

	hUser32 = GetModuleHandle(_T("USER32.DLL"));

	// Try to find SetLayeredWindowAttributes
	pSLWA = (SLWAProc)GetProcAddress(hUser32, (char *)"SetLayeredWindowAttributes");

	// The version of Windows running does not support translucent windows!
	if(pSLWA == 0)
		return FALSE;

	SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);

	// Make this window 70% alpha
	return pSLWA(hwnd, 0, (BYTE)((255 * percent) / 100), LWA_ALPHA);
}

BOOL RemoveWindowTrans(HWND hwnd)
{
	// Remove WS_EX_LAYERED from this window styles
	SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED);
	
	// Ask the window and its children to repaint
	RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);

	return TRUE;
}
